package com.woniu.zsd.user.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.dto.UserDto;
import com.woniu.dto.UserInfoDto;
import com.woniu.form.*;
import com.woniu.zsd.permission.Permission.Permission;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.user.entity.User;
import com.woniu.zsd.user.entity.UserInfo;
import com.woniu.zsd.user.service.IUserInfoService;

import com.woniu.zsd.user.service.IUserService;
import com.woniu.zsd.user.utils.UserTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/user-info")
@Api(tags = "用户信息管理")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private UserTokenUtil tokenUtil;
    @Autowired
    private IUserService userService;

    @GetMapping("/selUserInfo")
    @ApiOperation(value = "用户个人查询信息")
    public Result selUserInfo(@RequestHeader String token){
        System.out.println(token);
        String id = String.valueOf(tokenUtil.getUserId(token));
        System.out.println(id);
        UserInfoDto dto = userInfoService.selUserInfo(id);
        return Result.getResult(UserInfoDto.class).setData(dto).setMsg("查询成功");
    }

    @GetMapping("/selUser")
    @ApiOperation(value = "管理员条件查询用户")
    public Result<PageInfo<UserDto>> selUserById(@ApiParam(name = "suf", value = "用户查询表单") SelUserForm suf, @RequestHeader String token){
        PageInfo<UserDto> userDtoList  = userInfoService.selUser(suf);;
        return new Result<PageInfo<UserDto>>().setCode(200).setMsg("查询用户信息成功").setData(userDtoList);
    }

    @GetMapping("/selUserInfoByIds")
    @ApiOperation(value = "根据id批量查询用户信息")
    public Result<List<UserDto>> selUserInfoByIds(@RequestParam(value = "userIds") Set<Integer> userIds){
        System.out.println(userIds);
        List<UserDto> userDtoList = userInfoService.selUserInfoByIds(userIds);
        return Result.getListResult(UserDto.class).setData(userDtoList).setMsg("查询用户信息成功");
    }

    @PutMapping("/updateUserInfo")
    @ApiOperation(value = "修改用户信息")
    public Result updateUserInfo(@ApiParam(name = "uuif", value = "用户更新信息表单")@RequestBody UpdateUserInfoForm uuif,
                                 @RequestHeader String token) throws IOException {
        String id = String.valueOf(tokenUtil.getUserId(token));
        UserInfo userInfo = userInfoService.updateUserInfo(uuif,id);
        return Result.getResult(UserInfo.class).setData(userInfo).setMsg("修改用户信息成功");
    }

    @PutMapping("/updateUserPhone")
    @ApiOperation(value = "更换绑定手机号")
    public Result updateUserPhone(@ApiParam (name = "upf", value = "用户手机号修改表单")@RequestBody UpdatePhoneForm upf, @RequestHeader String token) {
        String id = String.valueOf(tokenUtil.getUserId(token));
        User user = userService.updateUserPhone(upf,id);
        return Result.getResult(User.class).setData(user).setMsg("换绑成功");
    }
    @PutMapping("/user/password")
    @ApiOperation(value = "修改密码")
    public Result updatePassword(@ApiParam (name = "upf", value = "用户密码修改表单")@RequestBody UpdatePwdForm upf,
            @RequestHeader String token){
        String id = String.valueOf(tokenUtil.getUserId(token));
        return userInfoService.updatePassword(upf,id);
    }


    @PutMapping("/addPointById")
    @ApiOperation(value = "修改用户积分")
    public Result addPointById(@RequestBody @ApiParam(name = "apf", value = "用户积分表单") AddPointForm apf){
        userInfoService.addPointById(apf);
        return Result.getResult(Boolean.class).setData(true).setMsg("增加积分成功");
    }
}